

# 计算x的n次方
print("计算x的n次方")
def cal(x,n):
    res = 1
    for i in range(0,n):
        res = res*x
    return res
print(cal(3,4))

print("计算x的阶乘")
def cal(x):
    res = 1
    for i in range(1,x+1):
        res=res*i
    print(res)
cal(5)

print("给定一个整数数组 nums 和一个整数目标值 target，请你在该数组中找出 和为目标值 target"
      "  的那 两个 整数，并返回它们的数组下标")
def twoSum(nums:list,target:int):
    for i in range(0,len(nums)):
        for j in range(i+1,len(nums)):
            if nums[i]+nums[j] == target:
                return [i,j]
nums = [3,2,4]
print(twoSum(nums,6))

print("给你一个整数 x ，如果 x 是一个回文整数，返回 true ；否则，返回 false ")
def isPalindrome(x:int):
    return str(x)==str(x)[::-1]
print(isPalindrome(-10))


print("罗马数字转整数")
def romanToInt(s:str)->int:
    rom_str=s.replace("IV","1").replace("IX","2").replace("XL","3").\
        replace("XC","4").replace("CD","5").replace("CM","6")
    int_num=0
    for s in rom_str:
        if s =="I":
            int_num=1+int_num
        elif s == "V":
            int_num+=5
        elif s == "X":
            int_num += 10
        elif s == "L":
            int_num += 50
        elif s == "C":
            int_num += 100
        elif s == "D":
            int_num += 500
        elif s == "M":
            int_num += 1000
        elif s == "1":
            int_num += 4
        elif s == "2":
            int_num += 9
        elif s == "3":
            int_num += 40
        elif s == "4":
            int_num += 90
        elif s == "5":
            int_num += 400
        elif s == "6":
            int_num += 900
    return int_num

print(romanToInt("LVIII"))


print("最长公共前缀")
def longestCommonPrefix(strs:list[str]):
    strs = sorted(strs,key=lambda s:len(s))
    prefix_str = ""
    for i in range(0,len(strs[0])):
        if len(set([s[i] for s in strs])) >1:
            break
        prefix_str = prefix_str+strs[0][i]
    return prefix_str
# strs = ["flower","flow","flight"]
strs = ["dog","racecar","car"]
print(longestCommonPrefix(strs))

print("有效的括号一")
def isValid(s:str) ->bool:
    for i in range(0,len(s)//2):
        s=s.replace("()","").replace("[]","").replace("{}","")
    print(s)
    return len(s) == 0
s="()[]{}"
print(isValid(s))

print("有效的括号二")
def isValid2(strs:str)->bool:
    stack = []
    test_map={")":"(",
              "]":"[",
              "}":"{"}
    for s in strs:
        if s in test_map:
            if len(stack) ==0 or test_map.get(s) != stack.pop():
                return False
        else:
            stack.append(s)
    return len(stack) ==0
s="[]"
print(isValid2(s))

print("删除有序数组中的重复项")
def removeDuplicates(nums:list[int]) ->int:
    return sorted(list(set(nums)))

nums = [0,0,1,1,1,2,2,3,3,4]
print(removeDuplicates(nums))